$(document).ready(function() {
    $widget_table.loadTables();
});

$widget_table = {
    loadTables : function() {
        $(".dmf-widget-table").each(function() {
            $widget_table.loadTable(this);
        });
        
        //Remove unnecessary javascript files (since there already loaded)
        $(".dmf-dependencies script").remove();
    },
    fixHeaders : function( table ) {
        var thHeaders = [];
        var tdHeaders = [];
        
        $(table).find(".dmf-table").find(".dmf-tr").eq(0).find(".dmf-td").each(function() {
            thHeaders.push( $(this).width() - 5 );
        });
        
        $(table).find(".dmf-headers").find(".dmf-tr").eq(0).find(".dmf-th").each(function() {
            tdHeaders.push( $(this).width() );
        });
        
        var headers = [];
        
        for( x in thHeaders )
        {
            if ( tdHeaders[x] > thHeaders[x] )
            {
                headers.push( tdHeaders[x] );
            }
            else
            {
                headers.push( thHeaders[x] );
            }
        }
        
        var i;

        $(table).find(".dmf-table").find(".dmf-tr").each(function() {
            i = 0;
            
            $(this).find(".dmf-td").each(function() {
                $(this).width( headers[i] );
                
                i++;
            });
        });
        
        var j;
        
        $(table).find(".dmf-headers").find(".dmf-tr").each(function() {
            j = 0;
            
            $(this).find(".dmf-th").each(function() {
                $(this).width( headers[j] );
                
                j++;
            });
        });
        
        var totalHeaderHeight = 0;
        
        $(table).find(".dmf-headers").find(".dmf-th").each(function() {
            totalHeaderHeight = totalHeaderHeight + $(this).width() + 14;
        });
        
        var difference = $(table).find(".dmf-headers").width() - totalHeaderHeight;
        
        $(table).find(".dmf-headers").find(".dmf-th").last().width( $(table).find(".dmf-headers").find(".dmf-th").last().width() + difference );
    },
    getAttr : function( table, index ) {
        var val = false;
        
        $(table).find(".dmf-widget-attributes div").each(function() {
            if ( this.className == index )
            {
                val = $(this).html();
            }
        });
        
        return val;
    },
    searchIn : function( table, query ) {
        $(table).find(".dmf-table").find("tr").show();
        $(table).find(".dmf-search-highlight").contents().unwrap();
        
        $(table).find(".dmf-table").find("tr").each(function() {
            var found = false;
            
            $(this).find("td").each(function() {
                if ( $(this).text().indexOf( query ) > -1 )
                {
                    found = true;
                }
                
                if ( $widget_table.getAttr( table, "search_highlight" ) == "true" && query.split("").length > 0 )
                {
                    $widget_table.domChange(this);
                    
                    $(this).html( this.innerHTML.replace( $(this).text(), $(this).text().replace( query, "<span class='dmf-search-highlight'>" + query + "</span>" ) ) );
                }
            });
        
           if ( ! found )
           {
               $(this).hide();
           }
        });
        
        this.setZebra(table);
    },
    domChange : function( element ) {
        if ( typeof( element.attributes ) == "undefined" || element.attributes == null )
        {
            return;
        }
        
        if ( element.checked )
        {
            element.setAttribute( "checked", true );
        }
        
        for (var i = 0; i < element.attributes.length; i++) {
           var attrib = element.attributes[i];
           
           if ( attrib.value != null )
           {
               element.setAttribute( attrib.name, attrib.value );
           }
        }
        
        for ( x in element.childNodes )
        {
            $widget_table.domChange( element.childNodes[x] );
        }
    },
    setZebra : function( table ) {
        var even = false;
        
        $(table).find(".dmf-table").find("tr").removeClass("dmf-table-even").removeClass("dmf-table-odd");
        
        $(table).find(".dmf-table").find("tr").each(function() {
            if ( $(this).is(":visible") )
            {
                even = ( even === true ) ? false : true;
            
                $(this).addClass("dmf-table-" + ( even ? "even" : "odd" ));
            }
        });
    },
    setWidth : function( table, width ) {
        $(table).find(".dmf-headers").width( width );
        $(table).find(".dmf-table").width( width - ( $(table).find(".dmf-scrollable-table").hasScrollBar() ? 17 : 0 ) );
        $(table).find(".dmf-scrollable-table").width( width );
        $(table).find(".dmf-table").width( width );
        
        this.fixHeaders( table );
    },
    setHeight : function( table, height ) {
        $(table).find(".dmf-scrollable-table").height( height );
    },
    setAttributes : function( table ) {
        if ( this.getAttr( table, "height" ) )
        {
            this.setHeight( table, this.getAttr( table, "height" ) );
        }
        
        if ( this.getAttr( table, "width" ) )
        {
            this.setWidth( table, this.getAttr( table, "width" ) );
        }
        else
        {
            this.fixHeaders( table );
        }
        
        if ( this.getAttr( table, "zebra") == "true" )
        {
            $widget_table.setZebra( table );
        }
        
        if ( this.getAttr( table, "highlight" ) == "true" )
        {
            $(table).find(".dmf-table").find("tr").each(function() {
                $(this).mouseover(function() {
                    $(this).addClass("dmf-table-highlight");
                });
                
                $(this).mouseout(function() {
                    $(this).removeClass("dmf-table-highlight");
                });
                
                $(this).click(function() {
                    $(this).find("input[type='checkbox']").each(function() {
                        $(this).attr( 'checked', ! $(this).attr( "checked" ) );
                    });
                });
            });
        }
        
        if ( this.getAttr( table, "search" ) == "true" )
        {
            $(table).find(".dmf-search-query").keyup(function() {
                $widget_table.searchIn( table, $(this).val() );
            });
            
            $(table).find(".dmf-search-query").keydown(function() {
                $widget_table.searchIn( table, $(this).val() );
            });
        }
    },
    setClickedInfo : function( table ) {
        var str = "";
        
        $(table).find("input[type='checkbox']").each(function() {
            if ( $(this).attr( "checked" ) )
            {
                str = str + $(this).attr("value") + "|";
            }
        });
        
        $(table).find("input[type='hidden']").val( str );
    },
    loadTableData : function( table ) {
        for ( x in $(table).find("input[type='hidden']").val().split("|") )
        {
            var nr = $(table).find("input[type='hidden']").val().split("|")[x];
            
            if ( nr.length > 0 )
            {
                $(table).find("input[value='" + nr + "']").attr( "checked", true );
            }
        }
    },
    loadTable : function( table ) {
        $(table).find(".dmf-widget-attributes").hide();
        
        $(table).find(".dmf-headers").width( $(table).find(".dmf-table").width() + 10 );
        $(table).find(".dmf-scrollable-table").width( $(table).find(".dmf-table").width() + 10 );
        
        this.setAttributes( table );
        
        $(table).width( $(table).find(".dmf-headers").width() );
        
        $(table).click(function() {
            $widget_table.setClickedInfo( table );
        });
        
        if ( $(table).find("input[type='hidden']").val().length > 0 )
        {
            this.loadTableData( table );
        }
        
        if ( this.getAttr( table, "resizable" ) == "true" )
        {
            $(table).resizable({ 
                helper: "dmf-table-resize-helper", 
                stop : function() {
                    $widget_table.setWidth( this, this.style.width );
                },
                start : function() {
                    $(".dmf-search-query").val(this.className);
                    $widget_table.setWidth( this, this.style.width );
                },
                resize : function() {
                    $widget_table.setWidth( this, this.style.width );
                }
            });
        }
    }
};